package service

import (
	"go.uber.org/zap"
	"gitee.com/carollia/data-sdk-go/internal"
	"gitee.com/carollia/data-sdk-go/model"
	"time"
)

var Logger, _ = zap.NewProduction()

// 用户信息
func (s *Service) profile(eventType, uid string, uidType string, properties map[string]interface{}) (err error) {
	defer func() {
		if e := recover(); e != nil {
			Logger.Error("s.profile Get Panic Error", zap.Any("error", e))
			return
		}
	}()
	data := &model.User{
		Type:       eventType,
		Uid:        uid,
		UidType:    uidType,
		Properties: properties,
	}
	err = s.d.Dispatch(data)
	return
}

// 物品信息
func (s *Service) item(eventType, itemId, itemType string, name string, price float32, properties map[string]interface{}) (err error) {
	defer func() {
		if e := recover(); e != nil {
			Logger.Error("s.item Get Panic Error", zap.Any("error", e))
			return
		}
	}()
	data := &model.Item{
		Type:       eventType,
		ItemId:     itemId,
		ItemType:   itemType,
		Name:       name,
		Price:      price,
		Properties: properties,
	}
	err = s.d.Dispatch(data)
	return
}

// 礼包信息
func (s *Service) bundle(eventType, bundleId, name string, price float32, items [][]int, properties map[string]interface{}) (err error) {
	defer func() {
		if e := recover(); e != nil {
			Logger.Error("s.bundle Get Panic Error", zap.Any("error", e))
			return
		}
	}()
	data := &model.Bundle{
		Type:       eventType,
		BundleId:   bundleId,
		Items:      items,
		Name:       name,
		Price:      price,
		Properties: properties,
	}
	err = s.d.Dispatch(data)
	return
}

func (s *Service) Ping() (err error) {
	// 请求 config.url + /ping 的路由
	url := s.c.Url + "/Ping"
	_, err = internal.DoHttpRequest(url, "GET", time.Second*3, model.RequestData{})
	return
}
